<?php

/**
 * @author PeoneEr
 * @package aion personal cabinet v1.0
 * @copyright Aion Factory Inc. 2010-2011
 * @license GNU v3
 * @link Ai-Factory.ru && http://code.google.com/p/aionprivateofficev2
 */
require_once "core/application/models/bans/mbans.php";
require_once "core/application/classes/errorClass.php";

class bans_Controller extends Bans_Model {

    private static $_instance = NULL;
    public $_lang             = NULL;
    private $_params       = array();

    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

    public function returnListBans() {
        /**
         * @return список банов
         */
        return $this->getListBans();
    }

    public function delban() {
        /**
         * удаление банов
         */
        $this->_lang = $this->getLanguage();
        /**
         * проверка $_GET['id']
         *  если не число - посылаем на три буквы, снабдив вазелином
         */
        if (!is_numeric($_GET['id']) || empty($_GET['id']) || !isset($_GET['id']) || is_null($_GET['id']))
            $this->echoError($this->_lang->language['requiredFields']);
        try {
            $this->del_ban($_GET['id']);
            $this->echoSuccess($this->_lang->language['banAreRemove']);
        } catch (Exception $e) {
            echo $e->getMessage();
            error::getInstance($this->_lang->language['defaultError']);
        }
    }

    public function addban() {
        /**
         * добавление бана
         */
        $this->_lang = $this->getLanguage();
        
        $this->_params = array(
            'ip' => $_POST['ip'],
            'time_end' => $_POST['time_end']
        );
        
        try {
            foreach ($this->_params as $key => $val) {
                if (empty($val) || !isset($val) || is_null($val)) {
                    unset($key);
                    $this->echoError($this->_lang->language['requiredFields']);
                }
            }
        } catch (Exception $exc) {
            echo $exc->getMessage();
        }
            /**
             * проверка на правильность IPшника
             */
        if (!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->_params['ip']))
            $this->echoError($this->_lang->language['invalidIPAddress']);
        if ($this->isBanned($this->_params['ip']))
            $this->echoError($this->_lang->language['isBanned']);
        try {
                $this->addNewBan($this->_params['ip'], $this->_params['time_end']);
                $this->echoSuccess($this->_lang->language['successAdd']);
                echo "<p>" . $this->_lang->language['else'] . " <a href='index?adm&add_ban'>Да</a> <a href='index?adm'>Нет</a></p>";
        } catch (Exception $e) {
                echo $e->getMessage();
                error::getInstance($this->_lang->language['defaultError']);
        }
        unset($this->_params);
    }

    public function infoAboutBan($id) {
        return $this->getInfoBanForItEdit($id);
    }

    public function editban_save() {
        $this->_lang = $this->getLanguage();
        $this->_params = array(
            'ip' => $_POST['mask'],
            'time_end' => $_POST['time_end'],
            'id' => $_POST['id']
        );
        try {
            foreach ($this->_params as $key => $val) {
                if (empty($val)) {
                    unset($key);
                    $this->echoError($this->_lang->language['requiredFields']);
                }
            }
        } catch (Exception $exc) {
            echo $exc->getMessage();
        }

        if (!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->_params['ip']))
            $this->echoError($this->_lang->language['invalidIPAddress']);
        try {
            $this->edit_ban($this->_params['ip'], $this->_params['time_end'], $this->_params['id']);
            $this->echoSuccess($this->_lang->language['success']);
        } catch (Exception $e) {
            echo $e->getMessage();
            error::getInstance($this->_lang->language['defaultError']);
        }
        unset($this->_params);
    }

    public function delAllBans() {
        $this->_lang = $this->getLanguage();
        try {
            $this->deleteAllBannedIPsFromBD();
            $this->echoSuccess($this->_lang->language['allBansAreDeleted']);
        } catch (Exception $e) {
            echo $e->getMessage();
            error::getInstance($this->_lang->language['defaultError']);
        }
    }

}

?>